package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.domain.Result;
import com.he.domain.dto.CategoryDTO;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.vo.CategoryOptionVO;
import com.he.service.CategoryService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/18 8:01
 * @Desc 分类数量控制器
 */
@Tag(name = "分类模块")
@RestController
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Operation(description = "查看分类列表")
    @AccessLimit(seconds = 1, maxCount = 5)
    @GetMapping("/category/list")
    public Result<?> listCategoryVO() {
        return Result.success(categoryService.listCategoryVO());
    }

    @Operation(description = "查看后台分类列表")
    @AccessLimit(seconds = 1, maxCount = 3)
    @PreAuthorize("@ps.hasPermission('blog:category:list')")
    @GetMapping("/admin/category/list")
    public Result<?> listCategoryBackVO(ConditionDTO conditionDTO) {
        return Result.success(categoryService.listCategoryBackVO(conditionDTO));
    }

    /**
     * 添加分类
     *
     * @param category 分类信息
     * @return {@link Result<>}
     */
    @Operation(description = "添加分类")
    @PreAuthorize("@ps.hasPermission('blog:category:add')")
    @PostMapping("/admin/category/add")
    public Result<?> addCategory(@Validated @RequestBody CategoryDTO category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param category 分类信息
     * @return {@link Result<>}
     */
    @Operation(description = "修改分类")
    @PreAuthorize("@ps.hasPermission('blog:category:update')")
    @PutMapping("/admin/category/update")
    public Result<?> updateCategory(@Validated @RequestBody() CategoryDTO category) {
        categoryService.updateCategory(category);
        return Result.success();
    }


    /**
     * 删除分类
     *
     * @param categoryIdList 分类id集合
     * @return {@link Result<>}
     */
    @Operation(description = "删除分类")
    @PreAuthorize("@ps.hasPermission('blog:category:delete')")
    @DeleteMapping("/admin/category/delete")
    public Result<?> deleteCategory(@RequestBody List<Integer> categoryIdList) {
        categoryService.deleteCategory(categoryIdList);
        return Result.success();
    }


    @Operation(description = "查看分类文章")
    @AccessLimit(seconds = 1, maxCount = 5)
    @GetMapping("/category/article/list")
    public Result<?> listCategoryArticleVO(ConditionDTO conditionDTO) {
        return Result.success(categoryService.listCategoryArticleVO(conditionDTO));
    }

    /**
     * 查看分类选项
     *
     * @return {@link Result<CategoryOptionVO>} 分类列表
     */
    @Operation(description = "查看分类选项")
    @AccessLimit(seconds = 1, maxCount = 3)
    @GetMapping("/admin/category/option")
    public Result<List<CategoryOptionVO>> listCategoryOption() {
        return Result.success(categoryService.listCategoryOption());
    }
}
